package com.dfrz.stockMarket.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.Session;
import org.apache.catalina.mbeans.UserMBean;

import com.dfrz.stockMarket.bean.MessageBean;
import com.dfrz.stockMarket.bean.UserBean;
import com.dfrz.stockMarket.cons.Constants;
import com.dfrz.stockMarket.dao.UserInfoDao;
import com.dfrz.stockMarket.model.UserInfoModel;
import com.dfrz.stockMarket.service.UserService;

import javafx.scene.control.Alert;

public class LoginActionCtrl extends BaseController {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String path = req.getContextPath();
		String name=req.getParameter("name");
		String pwd=req.getParameter("pwd");
		System.out.println("path:"+path);
		try {
			
			
			MessageBean mb=UserService.getUserBeanByName(name);
			UserBean user=(UserBean)mb.getMsg();
			String uname=user.getUserInfoModel().getUserName();
			String upwd=user.getUserInfoModel().getUserName();
			System.out.println("name:"+uname);
			System.out.println("pwd:"+upwd);
				if(uname==null||uname==""){
					System.out.println("账号不存在！");
					/*req.getRequestDispatcher("/page/PageCtrl.php").forward(req, resp);*/
				}else{
					if(pwd==upwd||pwd.equals(upwd)){
						
						req.setAttribute("path", path);
						req.setAttribute("uname", name);
						setSession(req, "msg", new MessageBean(true,Constants.USER_SUCCESS_TYPE2));
						setSession(req, Constants.USER_CONTEXT, (UserBean)UserService.getUserBeanByName(uname).getMsg());
						req.getRequestDispatcher("/page/PageCtrl.php").forward(req, resp);
						
					}else{
						System.out.println("密码错误！");
						/*req.getRequestDispatcher("/page/PageCtrl.php").forward(req, resp);*/
						
					}
				
				}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	
		}
	
	}


